// VSCode 适配层 - 使用暴露的 API，手动解析模式
(function() {
  'use strict';
  
  // 获取 VSCode API
  const vscode = typeof acquireVsCodeApi !== 'undefined' ? acquireVsCodeApi() : null;
  const isVSCodeEnvironment = !!vscode;
  
  // 调试日志函数
  function log(message) {
    if (isVSCodeEnvironment) {
      console.log('[DBC Editor] ' + message);
    }
  }
  
  // 检查并设置界面元素
  function initializeUI() {
    log('初始化UI');
    const openButton = document.getElementById('dbc-open-button');
    const downloadButton = document.getElementById('dbc-download-button');
    
    if (!openButton) {
      log('错误: 找不到open按钮');
      return false;
    }
    
    if (!downloadButton) {
      log('错误: 找不到download按钮');
      return false;
    }
    
    return true;
  }
  
  // 初始化适配器
  function initialize() {
    log('初始化DBC编辑器适配器 - 手动模式');
    
    // 检查dbc-editor.js中的API是否已经暴露
    if (!window.dbcEditorApi) {
      log('等待API就绪...');
      setTimeout(initialize, 100);
      return;
    }
    
    // 设置按钮事件处理
    const openButton = document.getElementById('dbc-open-button');
    if (openButton) {
      log('设置open按钮点击事件');
      openButton.addEventListener('click', function() {
        log('手动解析按钮被点击');
        if (window.dbcEditorApi && typeof window.file !== 'undefined') {
          log('解析文件，长度: ' + window.file.length);
          window.dbcEditorApi.setFile(window.file, window.filename || 'unknown.dbc');
          window.dbcEditorApi.parse();
          log('文件解析完成');
        } else {
          log('错误: API或文件内容不可用');
        }
      });
    }
    
    // 设置保存按钮事件处理
    const downloadButton = document.getElementById('dbc-download-button');
    if (downloadButton) {
      log('设置download按钮点击事件');
      downloadButton.addEventListener('click', function(event) {
        event.preventDefault();
        if (typeof downloadDbc === 'function') {
          const content = downloadDbc();
          vscode.postMessage({
            type: 'save',
            content: content,
            filename: window.filename || 'modified.dbc'
          });
        }
      });
    }
  }

  // 处理VSCode消息
  if (isVSCodeEnvironment) {
    log('VSCode环境已检测，设置消息监听');
    
    // 监听来自VSCode的消息
    window.addEventListener('message', function(event) {
      const message = event.data;
      
      if (message.type === 'updateContent') {
        log('收到文件更新: ' + (message.filename || 'unknown') + ', 长度: ' + message.content.length);
        window.file = message.content;
        window.filename = message.filename;
      } else if (message.type === 'parseFile') {
        // 处理从快捷键触发的解析命令
        log('收到快捷键解析命令');
        const openButton = document.getElementById('dbc-open-button');
        if (openButton) {
          log('模拟点击open按钮');
          openButton.click();
        } else {
          log('错误: 无法找到open按钮');
          // 尝试直接调用API
          if (window.dbcEditorApi && typeof window.file !== 'undefined') {
            log('直接调用API解析文件');
            window.dbcEditorApi.setFile(window.file, window.filename || 'unknown.dbc');
            window.dbcEditorApi.parse();
            log('文件解析完成');
          }
        }
      }
    });
  }

  // 如果DOM已加载完成，立即初始化
  if (document.readyState === 'complete' || document.readyState === 'interactive') {
    log('页面已加载，开始初始化');
    setTimeout(initialize, 100);
  } else {
    // 否则等待DOM加载完成
    log('等待DOM加载完成');
    document.addEventListener('DOMContentLoaded', function() {
      log('DOM已加载完成');
      setTimeout(initialize, 100);
    });
  }
})();